/*
 *    Filename:	CircularSliderInspector.m 
 *    Created :	Thu Oct 22 00:46:47 1992 
 *    Author  :	Vince DeMarco
 *		<vince@whatnxt.cuc.ab.ca>
 *    LastEditDate was "Sun Nov 15 21:05:03 1992"
 *
 *    Modified by Laurent Daudelin <laurent@planon.qc.ca> to
 *    add support for lower and upper stop values.
 *    LastEditDate was "Tus Sep 27 10:00:00 1994"
 *
 * Copyright (C) 1995 Vince DeMarco
 * Use is governed by the MiscKit license
 */


#import "MiscCSInspector.h"
#import "MiscCircularSlider.h"

@implementation MiscCircularSliderInspector

- init
{
    char buf[MAXPATHLEN + 1];
    id bundle = [NXBundle bundleForClass:[MiscCircularSlider class]];

    [bundle getPath:buf forResource:"MiscCSInspector" ofType:"nib"];
    [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
	[[NXColorPanel sharedInstance:YES] setShowAlpha:YES];
    return self;
}

- ok:sender
{
    [object display];
    return [super ok:sender];
}

- update
{
	[typeMatrix selectCellWithTag:[object sliderStyle]];
	[tagField setIntValue:[object tag]];
	[widthField setFloatValue:[object sliderPathWidth]];
	[minValueField setFloatValue:[(Slider *)object minValue]];
	[maxValueField setFloatValue:[(Slider *)object maxValue]];
	[upperStopValueField setFloatValue:[object upperStopValue]];
	[lowerStopValueField setFloatValue:[object lowerStopValue]];
	[currentValueField setFloatValue:[object floatValue]];
	[startAngleField setFloatValue:[object startAngle]];
	[useBoundariesSwitch setState:[object useBoundaries]];
	[upperStopValueField setEnabled:[object useBoundaries]];
	[lowerStopValueField setEnabled:[object useBoundaries]];
	[[optionMatrix cellAt:0:0] setState:[object isContinuous]];
	[[optionMatrix cellAt:1:0] setState:![object isEnabled]];
	[[optionMatrix cellAt:3:0] setState:[object jumpToMousePoint]];
	[[optionMatrix cellAt:4:0] setState:[object isClockwise]];
    if ([object hidden] == YES) {
		[[optionMatrix cellAt:2:0] setState: 1]; /* If hidden it is disabled */
		[[optionMatrix cellAt:1:0] setEnabled:NO];
	} else {
		[[optionMatrix cellAt:2:0] setState: 0];
		[[optionMatrix cellAt:1:0] setEnabled:YES];
	}
	[borderMatrix selectCellWithTag:[object borderType]];
	[BGColorWell setColor:[object backgroundColor]];
	
	[optionMatrix display];
	[borderMatrix display];
	return self;
}

- revert:sender
{
	[self update];
	return [super revert:sender];
}

- (BOOL)wantsButtons
{
	return NO;
}

- setType:sender
{
	[object setSliderStyle:[[sender selectedCell] tag]];
	[self touch:self];
	return [self ok:sender];
}

- setMaximum:sender
{
	[object setMaxValue:[sender floatValue]];
	if ([sender floatValue] < [upperStopValueField floatValue])
	{
		[object setUpperStopValue:[sender floatValue]];
		[upperStopValueField setFloatValue:[sender floatValue]];
	}
	[self touch:self]; [self update];
	return [self ok:sender];
}

- setMinimum:sender
{
	[object setMinValue:[sender floatValue]];
	if ([sender floatValue] > [lowerStopValueField floatValue])
	{
		[object setLowerStopValue:[sender floatValue]];
		[lowerStopValueField setFloatValue:[sender floatValue]];
	}
	[self touch:self]; [self update];
	return [self ok:sender];
}

- setLowerStop:sender
{
	if ([sender floatValue] >= [object upperStopValue])
	{
		if ([object upperStopValue] - 5.0 < 0)
			[object setLowerStopValue:0.0];
		else
			[object setLowerStopValue:[object upperStopValue] - 5.0];
	}
	else if ([sender floatValue] < [minValueField floatValue])
		[object setLowerStopValue:[minValueField floatValue]];
	else
		[object setLowerStopValue:[sender floatValue]];
	if ([object lowerStopValue] > [currentValueField floatValue])
		[currentValueField setFloatValue:
			(([object upperStopValue] - [object lowerStopValue]) / 2) + [object lowerStopValue]];
	[self touch:self]; [self update];
	return [self ok:sender];
}

- setUpperStop:sender
{
	if ([sender floatValue] <= [object lowerStopValue])
	{
		if ([object lowerStopValue] + 5.0 > 360.0)
			[object setUpperStopValue:360.0];
		else
			[object setUpperStopValue:[object lowerStopValue] + 5.0];
	}
	else if ([sender floatValue] > [maxValueField floatValue])
		[object setUpperStopValue:[maxValueField floatValue]];
	else
		[object setUpperStopValue:[sender floatValue]];
	if ([object upperStopValue] < [currentValueField floatValue])
		[currentValueField setFloatValue:
			(([object upperStopValue] - [object lowerStopValue]) / 2) + [object lowerStopValue]];
	[self touch:self]; [self update];
	return [self ok:sender];
}

- setUsingBoundaries:sender
{
	if ([sender state])
		[object setUseBoundaries:YES];
	else
		[object setUseBoundaries:NO];
	[self touch:self]; [self update];
	return [self ok:sender];
}
		
- setCurrent:sender
{
	if ([sender floatValue] > [object upperStopValue])
		[object setFloatValue:[object upperStopValue]];
	else if ([sender floatValue] < [object lowerStopValue])
		[object setFloatValue:[object lowerStopValue]];
	else
		[object setFloatValue:[sender floatValue]];
	[self touch:self]; [self update];
	return [self ok:sender];
}

- setOptions:sender
{
	int state = [[sender selectedCell] state];
	switch ([[sender selectedCell] tag]){
	case 1: /* Cont */
		if (state)
			[object setContinuous:YES];
		else
			[object setContinuous:NO];
		break;
	case 2:  /* Disabled */
		if (state)
			[object setEnabled:NO];
		else
			[object setEnabled:YES];
		break;
	case 3:  /* Hidden */
		if (state){
			[object setHidden:YES]; /* If Hidden it is also disabled */
			[object setEnabled:NO];
			[[optionMatrix cellAt:1:0] setEnabled:NO];
			[[optionMatrix cellAt:1:0] setState:YES];
		} else {
			[object setHidden:NO];
			[[optionMatrix cellAt:1:0] setEnabled:YES];
	    }
		[optionMatrix display];
		break;
	case 4:  /* Jumps to mouse click */
		if (state)
			[object setJumpToMousePoint:YES];
		else
			[object setJumpToMousePoint:NO];
		break;
	case 5:  /* Goes clockwise */
		if (state)
			[object setClockwise:YES];
		else
			[object setClockwise:NO];
		break;
	}    
	[self touch:self];
	return [self ok:sender];
}

- setBorder:sender
{
	[object setBorderType:[[sender selectedCell] tag]];
	[self touch:self];
	return [self ok:sender];
}

- setTag:sender
{
	[(Cell *)object setTag:[sender intValue]];
	[self touch:self];
	return [self ok:sender];
}

- changeWidth:sender
{
	[object setSliderPathWidth:[sender floatValue]];
	[self touch:self];
	return [self ok:sender];
}

- setBGColor:sender
{
	[object setBackgroundColor:[sender color]];
	[self touch:self];
	return [self ok:sender];
}

- setStartAnglePos:sender
{
	[object setStartAngle:[sender floatValue]];
	[self touch:self];
	return [self ok:sender];
}

@end
